<?php

/* @var $this yii\web\View */

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\HtmlPurifier;
use yii\grid\GridView;
use app\models\Notice;
use yii\data\ActiveDataProvider;

$dataProvider = new ActiveDataProvider([
    'query' => Notice::find()->orderBy(['dt'=>SORT_DESC])->with('author0'),
    'pagination' => [
        'pageSize' => 20,
    ],
]);
$this->title = '向会员发布系统公告';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="page-title">
  <div>
    <h1><i class="fa fa-bullhorn"></i> 系统公告</h1>
    <ul class="breadcrumb side">
      <li><i class="fa fa-home fa-lg"></i></li>
      <li class="active"><a href="#">系统公告</a></li>
    </ul>
  </div>
  <?php
  if($this->context->curUserIsAdmin){
   ?>
  <div><a class="btn btn-info btn-flat" href="<?=Url::toRoute('site/addnotice')?>"><i class="fa fa-lg fa-edit"></i> 发布公告</a></div>
  <?php } ?>
</div>

<div class='card'>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
// 数据提供者中所含数据所定义的简单的列
// 使用的是模型的列的数据
[
  'header'=>'<div class="btn-group pull-right">
    <a class="btn btn-info btn-sm del-notice"><i class="fa fa-trash-o"></i></a>
  </div>',
  'content'=>function ($model, $key, $index, $column) {
    $output = '<div class="block">'.
                '<div class="block_content">'.
                  '<h2 class="title">'.
                    Html::encode($model->title).
                  '</h2>'.
                  '<div class="byline" style="color:#888;">'.
                    '<span>发布时间：'.Yii::$app->formatter->asRelativeTime($model->dt, time()).'</span>'.
                    '<span style="padding-left:20px;">发布者：贝恩资本官方</span>'.
                  '</div>'.
                  '<p class="excerpt">'.HtmlPurifier::process($model->content).
                  '</p>'.
                '</div>'.
              '</div>';
      return $output;
  },
  ],
],
'tableOptions' => [
  'class' => 'table table-striped'
],
]);

 ?>
 </div>
 <?php $this->beginBlock('notice-js') ?>
 //删除消息
 $('.del-notice').click(function(){
   var mykeys = $('.grid-view').yiiGridView('getSelectedRows');
   $.ajax({
     url:"<?=Url::toRoute('site/delnotice')?>",
     type:"get",
     datatype:"json",
     cache:false,
     data:{
       keys:mykeys
     },
     success:function(data){
        var obj = JSON.parse(data);
        if(obj.errcode===0){
           //删除行
           var i=0;
           for (i = 0; i < mykeys.length; i++ )
           {
             $("tr[data-key='"+mykeys[i]+"']").remove();
           }
        }
     },
     error:function(jqXHR,textStatus,errorThrown){
       console.log(jqXHR);
       console.log(textStatus);
       console.log(errorThrown);
     }
   });
 });

<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['notice-js'],\yii\web\View::POS_END); ?>
